﻿var SettingsPage = function(pageContainer) {

    SettingsPage.superclass.constructor.call(this, pageContainer);

    var self = this;

    var Elements = {
        Connect: pageContainer.find("#auto-connect"),
        Notifications: pageContainer.find("#receive-notifications"),
        Languages: pageContainer.find(".language-select"),
        SaveSettings: pageContainer.find(".saveSettings"),
        CommunicationsSection: pageContainer.find("#communication-settings")
    };

    var Templates = {
        LanguageTemplate: pageContainer.find(".languageTemplate")
    };

    self.initialize = override(self.initialize, function() {
        self.Title = translate('Module_Title_Settings');
        app.translatePage(app.Pages.Settings);

        var authData = app.getAuthenticationData();
        if (authData && !authData.IsSAAS)
            Elements.CommunicationsSection.show();

        self.setLanguages();

        var settings = app.getCurrentSettings();
        Elements.Connect.attr('checked', settings.AutoLogin ? 'checked' : '');
        Elements.Notifications.attr('checked', settings.AutoNotifications ? 'checked' : '');
        Elements.Languages.val(settings.Language);
        Elements.SaveSettings.tap(function() {
            self.saveSettings();
        });

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(false);
        });

        pageContainer.find('.autoConnectLabel').tap(function() {
            if (Elements.Connect.is(":checked"))
                Elements.Connect.removeAttr('checked');
            else Elements.Connect.attr('checked', 'checked');
        });

        pageContainer.find('.receiveNotificationsLabel').tap(function() {
            if (Elements.Notifications.is(":checked"))
                Elements.Notifications.removeAttr('checked');
            else Elements.Notifications.attr('checked', 'checked');
        });
    });

    var translateAvailableLanguages = function() {
        Constants.AvailableLanguages[0].language = translate('General_LimbaRO');
    };

    this.saveSettings = function() {
        var settings = new Object();
        settings.AutoLogin = Elements.Connect.is(":checked");
        settings.AutoNotifications = Elements.Notifications.is(":checked");
        settings.Language = Elements.Languages.val();

        var mobilePlatformIndex = app.Phone.getMobilePlatformIndex();
        if (settings.AutoNotifications) {
            var username = app.User.IsAlsoEmployee ? null : app.User.Username;
            app.TMMService.PushMessaging.RegisterDevice(app.User.DeviceId, mobilePlatformIndex, Constants.ApplicationId, app.User.Company, username, app.User.PRS_ID, settings.Language);
        } else app.TMMService.PushMessaging.UnregisterDevice(app.User.DeviceId, mobilePlatformIndex, Constants.ApplicationId);

        app.showLoadingScreen();
        var currLang = app.getCurrentLanguage();
        app.setCurrentSettings(settings, function() {
            if (currLang != settings.Language) {
                app.TMMService.Security.ChangeLanguage(settings.Language, function(response) {
                    log(response.FinalResult);
                    if (response.FinalResult) {
                        var authData = app.getAuthenticationData();
                        authData.Token = response.FinalResult;
                        app.setAuthenticationData(authData);
                    }
                    location.reload();
                });
            } else {
                app.hideLoadingScreen();
                app.MessagesFactory.showToastSuccess(translate('General_Succes'), translate('Settings_SaveSuccessfull'));
            }
        });
    };

    this.setLanguages = function() {
        translateAvailableLanguages();
        var source = Templates.LanguageTemplate.html();
        var template = Handlebars.compile(source);
        html = "";
        for (var i = 0; i < Constants.AvailableLanguages.length; i++) {
            html += template(Constants.AvailableLanguages[i]);
        }
        Elements.Languages.html(html);
    };

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    self.initialize();
};
extend(SettingsPage, TMMobilePage);